
using Youz.Common.DBUtility;
using Youz.CreditRepay.Entity;
using System;
using System.Data;
using System.Data.SqlClient;
using System.Text;
using Youz.Common.Helper;
using System.Collections.Generic;

namespace Youz.CreditRepay.DAL
{
    /// <summary>
    /// CR_RepaymentPlan Data Class
    /// </summary>
    public partial class CR_RepaymentPlanDAL : BaseDAL
    {
		private DbHelperSQL DbHelperSQL = new DbHelperSQL();
        public string TableName = "CR_RepaymentPlan";

        public CR_RepaymentPlanDAL()
        {}

        #region  成员方法
        /// <summary>
        ///  新增一条数据
        /// </summary>
        public bool Add(CR_RepaymentPlan model)
        {
            StringBuilder strSql = new StringBuilder();
			
            strSql.Append("insert into [CR_RepaymentPlan](");
            strSql.Append("[Status],[BankCardID],[BillingAmount],[RepaymentAmount],[Fee],[BeginDate],[EndDate],[Quantity],[CreatedOn],[ModifiedOn],[MemberAccount],[RepayedAmount]");
            strSql.Append(") values (");
            strSql.Append("@Status,@BankCardID,@BillingAmount,@RepaymentAmount,@Fee,@BeginDate,@EndDate,@Quantity,@CreatedOn,@ModifiedOn,@MemberAccount,@RepayedAmount");
            strSql.Append(")");
			
            SqlParameter[] parameters = {
                  
                    new SqlParameter("@MemberAccount", model.MemberAccount),
                    new SqlParameter("@RepayedAmount", model.RepayedAmount),
                    new SqlParameter("@BankCardID", model.BankCardID),
                    new SqlParameter("@Status",model.Status),
                    new SqlParameter("@BillingAmount", model.BillingAmount),
                    new SqlParameter("@RepaymentAmount", model.RepaymentAmount),
                    new SqlParameter("@Fee", model.Fee),
                    new SqlParameter("@BeginDate", model.BeginDate),
                    new SqlParameter("@EndDate", model.EndDate),
                    new SqlParameter("@Quantity", model.Quantity),
                    new SqlParameter("@CreatedOn", DateTime.Now),
                    new SqlParameter("@ModifiedOn", DateTime.Now)
            };

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        ///  保存一条数据
        /// </summary>
        public bool Update(CR_RepaymentPlan model)
        {
            StringBuilder strSql = new StringBuilder();
			
            strSql.Append("update [CR_RepaymentPlan] set ");
            strSql.Append("[Status]=@Status,[BankCardID]=@BankCardID,[BillingAmount]=@BillingAmount,[RepaymentAmount]=@RepaymentAmount,[Fee]=@Fee,[BeginDate]=@BeginDate,[EndDate]=@EndDate,[Quantity]=@Quantity,[ModifiedOn]=@ModifiedOn");
            strSql.Append(" where [ID]=@ID");
			
            SqlParameter[] parameters = {
                  
                    new SqlParameter("@ID", model.ID),
                    new SqlParameter("@Status", model.Status),
                    new SqlParameter("@BankCardID", model.BankCardID),
                    new SqlParameter("@BillingAmount", model.BillingAmount),
                    new SqlParameter("@RepaymentAmount", model.RepaymentAmount),
                    new SqlParameter("@Fee", model.Fee),
                    new SqlParameter("@BeginDate", model.BeginDate),
                    new SqlParameter("@EndDate", model.EndDate),
                    new SqlParameter("@Quantity", model.Quantity),
                    new SqlParameter("@ModifiedOn", model.ModifiedOn)
            };

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        public bool UpdateStatus(int id, RepaymentPlanStatus status)
        {
            StringBuilder strSql = new StringBuilder();

            strSql.Append("update [CR_RepaymentPlan] set ");
            strSql.Append("[Status]=@Status,[ModifiedOn]=@ModifiedOn");
            strSql.Append(" where [ID]=@ID");

            SqlParameter[] parameters = {
                    new SqlParameter("@ID", id),
                    new SqlParameter("@Status", (int)status),
                    new SqlParameter("@ModifiedOn", DateTime.Now)
            };

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 提交计划
        /// </summary>
        /// <returns></returns>
        //public bool Submit(int id, RepaymentPlanStatus status)
        //{
        //    List<SqlAndParams> list = new List<SqlAndParams>();
        //    StringBuilder strSql = new StringBuilder();

        //    list.Add(new SqlAndParams
        //    {
        //        Sql = "update[CR_RepaymentPlan] set Status]= @Status,[ModifiedOn]=@ModifiedOn where[ID] = @ID",
        //        SqlParameters = new SqlParameter[] {new SqlParameter("@ID", id),
        //            new SqlParameter("@Status", (int)status),
        //            new SqlParameter("@ModifiedOn", DateTime.Now) }
        //    });

        //    list.Add(new SqlAndParams
        //    {   Sql= "insert into [Pay_Amount]([PayType],[Amount],[Remark],[IsEnabled],[CreateTime],[ModifyTime]) values (@PayType,@Amount,@Remark,@IsEnabled,@CreateTime,@ModifyTime)",
        //        SqlParameters =new SqlParameter[] { new SqlParameter("@PayType", model.PayType),
        //            new SqlParameter("@Amount", model.Amount),
        //            new SqlParameter("@Remark", model.Remark),
        //            new SqlParameter("@IsEnabled", model.IsEnabled),
        //            new SqlParameter("@CreateTime", DateTime.Now),
        //            new SqlParameter("@ModifyTime", DateTime.Now)}
        //    });
            
        //}

        /// <summary>
        /// 删除一条数据
        ///</summary>
        public bool Delete(Int32 ID)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from [CR_RepaymentPlan] ");
            strSql.Append(" where [ID]=@ID");
			
            SqlParameter[] parameters = {
			
                new SqlParameter("@ID", ID)             
              
            };

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public CR_RepaymentPlan Get(Int32 ID)
        {
            StringBuilder strSql = new StringBuilder();
			
            strSql.Append("select  top 1 * from [CR_RepaymentPlan] ");
            strSql.Append(" where [ID]=@ID");
			
            SqlParameter[] parameters = {
			
                new SqlParameter("ID", ID)             
              
            };

            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                return Get(ds.Tables[0].Rows[0]);
            }
            else
            {
                return null;
            }
        }

        public CR_RepaymentPlan Get(string memberAccount, Int32 ccID)
        {
            StringBuilder strSql = new StringBuilder();

            strSql.Append("select  top 1 * from [CR_RepaymentPlan] ");
            strSql.Append(" where [BankCardID]=@BankCardID and MemberAccount=@MemberAccount order by ID desc");

            SqlParameter[] parameters = {
                new SqlParameter("@MemberAccount", memberAccount),
                new SqlParameter("@BankCardID", ccID)

            };

            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                return Get(ds.Tables[0].Rows[0]);
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public CR_RepaymentPlan Get(DataRow row)
        {
            if (row == null) return null;

            CR_RepaymentPlan model = new CR_RepaymentPlan();
	
            
            if (row["ID"] != DBNull.Value)
            {
                model.ID = (Int32)row["ID"];
            }      
            
            if (row["BankCardID"] != DBNull.Value)
            {
                model.BankCardID = (Int32)row["BankCardID"];
            }      
            
            if (row["BillingAmount"] != DBNull.Value)
            {
                model.BillingAmount = (Decimal)row["BillingAmount"];
            }      
            
            if (row["RepaymentAmount"] != DBNull.Value)
            {
                model.RepaymentAmount = (Decimal)row["RepaymentAmount"];
            }      
            
            if (row["Fee"] != DBNull.Value)
            {
                model.Fee = (Decimal)row["Fee"];
            }      
            
            if (row["BeginDate"] != DBNull.Value)
            {
                model.BeginDate = (DateTime)row["BeginDate"];
            }      
            
            if (row["EndDate"] != DBNull.Value)
            {
                model.EndDate = (DateTime)row["EndDate"];
            }      
            
            if (row["Quantity"] != DBNull.Value)
            {
                model.Quantity = (Int32)row["Quantity"];
            }

            if (row["Status"] != DBNull.Value)
            {
                model.Status = (Int32)row["Status"];
            }

            if (row["CreatedOn"] != DBNull.Value)
            {
                model.CreatedOn = (DateTime)row["CreatedOn"];
            }      
            
            if (row["ModifiedOn"] != DBNull.Value)
            {
                model.ModifiedOn = (DateTime)row["ModifiedOn"];
            }      
            

            return model;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet Gets(string sqlWhere, params SqlParameter[] sqlparams)
        {
            StringBuilder strSql = new StringBuilder();
			
            strSql.Append("select * from [CR_RepaymentPlan] ");
            if (sqlWhere.Trim() != "")
            {
                strSql.Append(" where " + sqlWhere);
            }
            strSql.Append(" order by ID desc");

            return DbHelperSQL.Query(strSql.ToString(), sqlparams);
        }

        public DataTable GetsByMember(string memberAccount, int cardID)
        {
            return CommonHelper.GetDateTable(Gets("MemberAccount=@MemberAccount and BankCardID=@BankCardID and [Status]<>@Status1", new SqlParameter[] { new SqlParameter("@MemberAccount", memberAccount), new SqlParameter("@BankCardID", cardID), new SqlParameter("@Status1", RepaymentPlanStatus.Temp) }));
        }

        /// <summary>
        /// 获得前几行数据
        /// </summary>
        public DataSet Gets(int Top, string sqlWhere, string filedOrder)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ");
            if (Top > 0)
            {
                strSql.Append(" top " + Top.ToString());
            }
            strSql.Append(" *  FROM [CR_RepaymentPlan] ");
			
            if (sqlWhere.Trim() != "")
            {
                strSql.Append(" where " + sqlWhere);
            }
            if(filedOrder.Trim() != "")
            {
                strSql.Append(" order by " + filedOrder);
            }
			
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获取记录总数
        /// </summary>
        public int GetRecordCount(string sqlWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) FROM [CR_RepaymentPlan] ");
            if (sqlWhere.Trim() != "")
            {
                strSql.Append(" where " + sqlWhere);
            }
            object obj = DbHelperSQL.GetSingle(strSql.ToString());
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }

        /// <summary>
        /// 获得数据列表分页
        /// </summary>
        public DataTable Gets(string sqlWhere, int pageindex, int pagesize, out int total, params SqlParameter[] cmdParms)
        {
            StringBuilder strSql = new StringBuilder();

            strSql.Append(" SELECT * FROM [CR_RepaymentPlan]  ");
            if (sqlWhere.Trim() != "")
            {
                strSql.Append(" where " + sqlWhere);
            }

            int count = 0;
            DataTable dt = DbHelperSQL.GetPager(pageindex, pagesize, "" + strSql + "", "order by ID desc ", out count, cmdParms);
            total = count;
            return dt;
        }
        #endregion  成员方法

        public bool Save(DataTable dt)
        {
            DbHelperSQL.SqlBulkCopyByDatatable("CR_RepaymentPlanItem", dt);

            return true;
        }

        /// <summary>
        /// 获得用户的银行卡还款统计信息
        /// </summary>
        /// <param name="memberAccount"></param>
        /// <param name="cardID"></param>
        /// <returns></returns>
        public DataRow GetSummary(string memberAccount, int cardID)
        {
            return CommonHelper.GetDataRow(DbHelperSQL.Query(@"select isnull(sum([BillingAmount]),0) as BillingAmount,isnull(sum([RepaymentAmount]),0) as RepaymentAmount,isnull(sum([Fee]),0) as Fee,isnull(sum([Quantity]),0) as Quantity,isnull(sum(RepayedAmount),0) as RepayedAmount
                    from [dbo].[CR_RepaymentPlan] where MemberAccount=@MemberAccount and BankCardID=@BankCardID and [Status]=@Status", 
                    new SqlParameter[] { new SqlParameter("@MemberAccount", memberAccount), new SqlParameter("@BankCardID", cardID), new SqlParameter("@Status", (int)RepaymentPlanStatus.Runing) }));
        }
    }
}


  